package com.project.common.controller;

import com.project.common.entity.PageVo;
import com.project.common.entity.SysMenus;
import com.project.common.entity.SysUsers;
import com.project.common.service.SysUserService;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/SysUsers")
public class SysUsersController {
    @Autowired
    private SysUserService sysUserService;
    @PostMapping("/loginUser")
    @ResponseBody
    public String login(SysUsers sysUsers, HttpSession session){
//        System.out.println(sysUsers);
        SysUsers user = sysUserService.queryUserByName(sysUsers.getLoginId());
        if(user!=null && user.getPassword().equals(sysUsers.getPassword())){
            session.setAttribute("user",user);
            return "ok";
        }else{
            return "error";
        }
    }


    @PostMapping("/queryUserById.action")
    @ResponseBody
    public SysUsers queryUserById(HttpServletRequest request){
        //获取当前登录人
        SysUsers user = (SysUsers)request.getSession().getAttribute("user");

        Integer id = user.getId();

        return sysUserService.queryUserById(id);

    }


//    模糊查询分页账号
    @ResponseBody
    @PostMapping("/queryAllUser.action")
    public PageVo<SysUsers> queryAllUser(@RequestBody Map<String,String> map){
        PageVo<SysUsers> sysUsersPageVo = sysUserService.queryAllUser(map);
        return sysUsersPageVo;
    }

    @ResponseBody
    @PostMapping("/queryAllUser1.action")
    public List<SysUsers> queryAllUser1(){
        List<SysUsers> sysUsers = sysUserService.queryAllUser();
        return sysUsers;
    }

//    退出登录
    @GetMapping("/loginOut.action")
    @ResponseBody
    public Integer loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return 1;
    }

    //确认修改头像
    @PostMapping("/updateSysUserById.action")
    @ResponseBody
    public void updateSysUserById(HttpServletRequest request,@RequestParam(name = "photoPath") MultipartFile photoPath) throws IOException {
//        System.out.println(photoPath);
        SysUsers sysUsers = new SysUsers();
        //获取要把图片存储在的文件夹中的路径
        String realPath = request.getServletContext().getRealPath("img/log");
//        System.out.println(realPath);
        //获取随机文件名称
        String uuId = UUID.randomUUID().toString();
        //获取图片名称
        String filename = photoPath.getOriginalFilename();
        //截取图片扩展名
        String substring = filename.substring(filename.lastIndexOf("."));

        photoPath.transferTo(new File(realPath+"/"+uuId+substring));
        //把文件路径存在数据库中
        sysUsers.setPhotoPath("img/log/"+uuId+substring);

        SysUsers user = (SysUsers)request.getSession().getAttribute("user");
        //把当前登录人的 ID
        sysUsers.setId(user.getId());
        //确认修改
        sysUserService.updateByPrimaryKeySelective(sysUsers);

    }

    @PostMapping("/addUserACount")
    @ResponseBody
    public Integer addUserACount(HttpServletRequest request,@RequestParam(name = "photoPath") MultipartFile photoPath,@RequestParam(name = "loginId") String loginId,@RequestParam(name = "password") String password) throws IOException {
        List<SysUsers> sysUsers = sysUserService.queryAllUser();

        for (int i=0;i<sysUsers.size();i++){
            if (sysUsers.get(i).getLoginId().equals(loginId)){
                return 0;
            }
        }

        SysUsers users = new SysUsers();
        //获取要把图片存储在的文件夹中的路径
        String realPath = request.getServletContext().getRealPath("img/log");
//        System.out.println(realPath);
        //获取随机文件名称
        String uuId = UUID.randomUUID().toString();
        //获取图片名称
        String filename = photoPath.getOriginalFilename();
        //截取图片扩展名
        String substring = filename.substring(filename.lastIndexOf("."));

        photoPath.transferTo(new File(realPath+"/"+uuId+substring));


        //把相应数据存在数据库中
        users.setPhotoPath("img/log/"+uuId+substring);
        users.setLoginId(loginId);
        users.setPassword(password);
        users.setStatus("1");

        //确认添加
        int i = sysUserService.insertSelective(users);
//        System.out.println(i);
        return i;
    }

    @GetMapping("/checkAddUserACount.action")
    @ResponseBody
    public Integer checkAddUserACount(String loginId){
        List<SysUsers> sysUsers = sysUserService.queryAllUser();

        for (int i=0;i<sysUsers.size();i++){
            if (sysUsers.get(i).getLoginId().equals(loginId)){
                return 0;
            }
        }
        return 1;
    }
}
